//
//  SLRecordListCell.swift
//  sulit-loan
//
//  Created by 王刚锋 on 2025/4/17.
//  Copyright © 2025 coolBoy. All rights reserved.
//

import UIKit
import SwiftyJSON

class SLRecordListCell: UITableViewCell {

    var json:JSON?
    var inc:[JSON] = []
    func setupUI(){
        self.bgImagV.isHidden = false
        self.backgroundColor = .clear
        self.contentView.backgroundColor = .clear
        self.selectionStyle = .none
        self.icon.layer.cornerRadius = 4
        self.dashedLine.isHidden = false
        self.applyButton.isHidden = false
    }
    
    lazy var dashedLine:SLDasheLine = {
        let v = SLDasheLine(frame: CGRect(x: 0, y: 44, width: DevConfig.width-30, height: 1))
        self.backView.addSubview(v)
        return v
    }()
    func setData(json: JSON){
        self.json = json
        self.icon.setUrlImage(url: json["commented"].stringValue)
        self.name.text = json["singularly"].stringValue
        self.applyButton.setTitle(json["civilizations"].stringValue, for: .normal)
        
        if json["imagined"].stringValue == ""{
            self.agreement.isHidden = true
        }else{
            self.agreement.isHidden = false
            // 设置部分文本带下划线
            let string = json["imagined"].stringValue
            let arr = string.components(separatedBy: "||")
            let text = arr.last!
            let attributedString = NSMutableAttributedString(string:text )
            let rangeNs = NSRange(location: 0, length: text.count)
            attributedString.addAttribute(.underlineStyle, value: NSUnderlineStyle.single.rawValue, range: rangeNs)
            attributedString.addAttribute(.foregroundColor, value: UIColor.systemBlue, range: rangeNs)
            self.agreement.attributedText = attributedString
        }
        
        let arr = json["colonialism"].arrayValue
        self.inc = arr
        self.tableView.reloadData()
        self.tableView.contentInset = .init(top: 10, left: 0, bottom: 0, right: 0)
        self.backView.snp.updateConstraints { make in
            make.left.equalTo(15)
            make.right.equalTo(-15)
            make.top.bottom.equalTo(0)
            make.height.equalTo(69+43+self.inc.count * 24)
        }
    }
    
    lazy var tableView:UITableView = {
        let pt = create_tableView(bgColor: .clear,supView: self.backView,style: .plain) { make in
            make.left.equalTo(15)
            make.right.equalTo(-15)
            make.top.equalTo(45)
            make.height.equalTo(self.inc.count * 24)
        }
        pt.isUserInteractionEnabled = false
        pt.rowHeight = 24
        pt.register(SLColonialismCell.self, forCellReuseIdentifier: "SLColonialismCell")
        pt.delegate = self
        pt.dataSource = self
        pt.bounces = false
        return pt
    }()
    @objc func buttonClick(btn: UIButton){
        let lumpur = self.json!["lumpur"].stringValue
        jump(url: lumpur)
    }
    lazy var backView: UIView  = {
        let v = UIView()
        v.backgroundColor = rgba(hex: "#F3F6F9")
        v.layer.masksToBounds = true
        v.layer.cornerRadius = 10
        self.contentView.addSubview(v)
        
        return v
    }()
    
    lazy var bgImagV:UIImageView = {
        let imgV = UIImageView()
        imgV.image = IMAGE("orderCell_bg")
        imgV.contentMode = .scaleToFill
        
        self.backView.addSubview(imgV)
        self.backView.sendSubviewToBack(imgV)
        imgV.snp.makeConstraints { make in
            make.left.right.top.bottom.equalTo(0)
        }
        return imgV
    }()
    lazy var icon:UIImageView = {
        let imgV = create_imageView(image: IMAGE("image1"), supView: self.backView) { make in
            make.width.equalTo(24)
            make.height.equalTo(24)
            make.left.equalTo(15)
            make.top.equalTo(9)
        }
        return imgV
    }()
    lazy var name:UILabel = {
        let label = create_label(text: "",textColor: .black,textFont: SLFontConfig.medium16, supView: self.backView) { make in
            make.left.equalTo(self.icon.snp.right).offset(8)
            make.centerY.equalTo(self.icon.snp.centerY)
        }
        return label
    }()
    
   
    
    lazy var applyButton:UIButton = {
        let btn = create_button(title:"Apply",titleColor: .white,titleFont: SLFontConfig.bold16,bgColor:rgba(hex: "#1608EC"), image: nil, supView: self.backView, closure: { make in
            make.centerX.equalToSuperview()
            make.bottom.equalTo(-11)
            make.width.equalTo(280)
            make.height.equalTo(36)
        }, selector: nil)
        btn.isUserInteractionEnabled = false
        btn.layer.masksToBounds = true
        btn.layer.cornerRadius = 18
        return btn
    }()

    lazy var agreement:UILabel = {
        let v = create_label(text: "",textColor: rgba(hex: "#1608EC"),textFont: SLFontConfig.medium14, supView: self.backView) { make in
            make.centerY.equalTo(self.icon.snp.centerY)
            make.right.equalTo(-16)
        }
        v.isUserInteractionEnabled = true
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(agreementClick))
        v.addGestureRecognizer(tapGesture)
        return v
    }()
    
    
    @objc func agreementClick(){
        let lumpur = self.json!["scholar"].stringValue
        jump(url: lumpur)
        
    }
}
extension SLRecordListCell:UITableViewDelegate,UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.inc.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let json = self.inc[indexPath.row]
        let cell = tableView.dequeueReusableCell(withIdentifier: "SLColonialismCell", for: indexPath) as! SLColonialismCell
        cell.selectionStyle = .none
        cell.contentView.backgroundColor = .clear
        cell.backgroundColor = .clear
        cell.leftL.text = json["himalayas"].stringValue
        cell.rightL.text = json["turned"].stringValue
        return cell
    }
}
class SLColonialismCell:UITableViewCell {
    lazy var leftL:UILabel = {
        let label = create_label(text: "",textColor: rgba(hex: "#8F8F8F"),textFont: SLFontConfig.medium15, supView: self.contentView) { make in
            make.left.equalTo(0)
            make.centerY.equalToSuperview()
            make.height.equalTo(14)
        }
        return label
    }()
    lazy var rightL:UILabel = {
        let label = create_label(text: "",textColor: .black,textFont: SLFontConfig.medium16, supView: self.contentView) { make in
            make.right.equalTo(0)
            make.centerY.equalToSuperview()
            make.height.equalTo(14)
        }
        return label
    }()
}
